<?php

namespace App\Kit;

use Firebase\JWT\JWT;
use Firebase\JWT\Key;
use Illuminate\Support\Facades\Redis;

class Token
{
    private static $key = "eml-secret";
    private static $alg = "HS256";
    private static $keyId = "eml";

    public static function createToken($data, $id, $iss,  $expire = 7200){
        $payload = [
            'iss' => $iss,
            'sub' => $id,
            'iat' => time(),
            'exp' => time() + $expire,
            'data' => $data
        ];
        return JWT::encode($payload, self::$key, self::$alg, self::$keyId);
    }

    public static function decodeToken($token){
        try {
            $payload = JWT::decode($token, new Key(self::$key, self::$alg));
        } catch(\Exception $e) {
            return false;
        }

        return $payload;
    }

    public static function set_admin_token($token, $id, $expire = 7200) {
        Redis::setEx(RedisKey::admin_token($id), $expire, $token);
    }

    public static function get_admin_token($id) {
        $res = Redis::get(RedisKey::admin_token($id));
        return $res;
    }

    public static function get_csutomer_token($id) {
        $res = Redis::get(RedisKey::customer_token($id));
        return $res;
    }

    public static function set_customer_token($token, $id, $expire = 7200) {
        Redis::setEx(RedisKey::customer_token($id), $expire, $token);
    }

    public static function get_api_token($id) {
        $res = Redis::get(RedisKey::api_token($id));
        return $res;
    }

    public static function set_api_token($token, $id, $expire = 7200) {
        Redis::setEx(RedisKey::api_token($id), $expire, $token);
    }

}
